package com.byCreatetion.adabter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;


import com.byCreation.t_globe.R;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;



import Myclass.AllIntent;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ToggleButton;

/**
 * 
 * @author javatechig {@link http://javatechig.com}
 * 
 */
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
	private Context context;
	private int layoutResourceId;
	

	

	     private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
		 private ImageLoader imageLoader = ImageLoader.getInstance();
		 private DisplayImageOptions options;
	
		 private ArrayList<HashMap<String, Object>> MyArrList;
	
		 
		 private int[] id_img={R.drawable.cards_list1,R.drawable.cards_list2,R.drawable.cards_list3,
				 R.drawable.cards_list4,R.drawable.cards_list5,};
			
		 
		 
		 
		 private int[] id_img_icon={R.drawable.check_toggle1,
				 R.drawable.check_toggle2,
				 R.drawable.check_toggle3,
				 R.drawable.check_toggle4,
				 R.drawable.check_toggle5,
				 R.drawable.check_toggle6,
				 R.drawable.check_toggle7,
				 R.drawable.check_toggle8,
				 R.drawable.check_toggle9,};
		 
		 
		 
	public GridViewAdapter(Context context, int layoutResourceId,ArrayList<HashMap<String, Object>> MyArrList) {
		super(context, layoutResourceId );
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.MyArrList=MyArrList;

		  options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.ic_launcher)
			.showImageForEmptyUri(R.drawable.ic_launcher)
			.showImageOnFail(R.drawable.ic_launcher)
			.delayBeforeLoading(100)
	        .cacheInMemory()
			.cacheOnDisc()
			.bitmapConfig(Bitmap.Config.RGB_565)
			.build();
		initImageLoader(this.context);
		
		
		
		Log.e("TG", " GridViewAdapter");
		
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder = null;
		
		Log.e("TG", "getView="+position);
		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);
			holder = new ViewHolder();
			holder.txt_title = (TextView) row.findViewById(R.id.txt_title);
			holder.toggle_icon=(ToggleButton)row.findViewById(R.id.toggle_icon);
			row.setTag(holder);
		} else {
			holder = (ViewHolder) row.getTag();
		}

		holder.toggle_icon.setBackgroundResource(id_img_icon[position]);

		
		
		
		return row;
		
	}

	static class ViewHolder {
		TextView txt_title;
		ImageView image;
		ToggleButton toggle_icon;
	}

	@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return MyArrList.size();// more than zero
    }
	
	public static void initImageLoader(Context context) {
	  		// This configuration tuning is custom. You can tune every option, you may tune some of them, 
	  		// or you can create default configuration by
	  		//  ImageLoaderConfiguration.createDefault(this);
	  		// method.
	  		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
	  		.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
	        .threadPoolSize(3) // default
	        .threadPriority(Thread.NORM_PRIORITY - 1) // default
	        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
	        .denyCacheImageMultipleSizesInMemory()
	        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
	        .memoryCacheSize(2 * 1024 * 1024)
	        .discCacheSize(50 * 1024 * 1024)
	        .discCacheFileCount(100)
	        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
	        .imageDownloader(new BaseImageDownloader(context)) // default
	        .imageDecoder(new BaseImageDecoder()) // default
	        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
	        .build();
			     
	  		// Initialize ImageLoader with configuration.
	  		ImageLoader.getInstance().init(config);
	  	}
	  	
	    
	@SuppressWarnings("unused")
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

			static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				if (loadedImage != null) {
					ImageView imageView = (ImageView) view;
					boolean firstDisplay = !displayedImages.contains(imageUri);
					if (firstDisplay) {
						FadeInBitmapDisplayer.animate(imageView, 500);
						displayedImages.add(imageUri);
					}
				}
			}
		}
		

}